﻿using System.Windows.Forms;
using Osg;
using ZFGK.Addins;
using ZFGK.OSG.UI;
using ZFGK.OSG.Utility;

namespace OSS.ModelViewer.Commands.TestCmds
{
    public class AutoTransformTestCmd : IExtendCommand
    {
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
            var viewForm = cmdData.ViewForm as IViewForm;
            if (viewForm == null)
                return CmdResult.Cancel;
            var form = viewForm as Form;
            var osgViewCtrl = viewForm.View as ZfOsgViewCtrl;

            var rectWidth = 20.0f;
            var verts = new Vec3d(-rectWidth * 0.5f, -rectWidth * 0.5f, 0).NewList().Right(rectWidth).Front(rectWidth).Left(rectWidth).Back(rectWidth);

            var line = OsgGeometryUtil.CreateLine(verts.ToVec3f(), new Vec3f(0, 0, 1), new Vec4f(1, 1, 0, 1)).CreateGeode();
            line.getOrCreateStateSet().SetLighting(false);

            int index = 0;
            foreach (var vec3d in verts)
            {
                var autoTransform = new AutoTransform();
                autoTransform.setPosition(vec3d);
                autoTransform.setAutoRotateMode(Osg.AutoTransform.AutoRotateMode.ROTATE_TO_SCREEN);
                autoTransform.setAutoScaleToScreen(true);
                autoTransform.setMinimumScale(0);
                autoTransform.setMaximumScale(float.MaxValue);
                autoTransform.addChild(line);
                //atf.setNodeMask(0); // 不显示
                autoTransform.getOrCreateStateSet().setRenderBinDetails(1000, "RenderBin");
                autoTransform.Name = index++.ToString();
                osgViewCtrl.OsgObj.AddOrReplaceModel("Test", autoTransform);
            }
            return CmdResult.Succeed;
        }
    }
}
